<?php
// +----------------------------------------------------------------------
// | 文章
// +----------------------------------------------------------------------
// | Author: 李乾坤 <261001642@qq.com>
// +----------------------------------------------------------------------

class ArticleAction extends BaseAction {
    function _initialize(){
        parent::_initialize();
        $Article=M('Article');
        $this->assign("NearList",$Article->where('status=1')->Order('hit asc')->limit(10)->select());
        
        $Comment=M('Comment');
        $this->assign("NearComment",$Comment->where('status=1')->Order('id asc')->limit(10)->select());
        
        $Cate=M('Cate');
        $this->assign("CateList",$Cate->where('status=1')->Order('id asc')->select());
        
        $Link=M('Link');
        $this->assign("LinkList",$Link->where('status=1')->Order('id asc')->limit(20)->select());
        
        $Tag=M('Tag');
        $this->assign("TagList",$Tag->where('module="Article"')->Order('count asc')->limit(20)->select());
    }
    public function index(){
    	$this->category();
    }
    public function category($id=0){
    	if ($id==0){
    		$id=intval($_REQUEST['id']);
    	}
    	$where='status=1';
    	if ($id!=0){
    		$where.=' and cid='.$id;
    	}
		$pagetitle=GTC($cid,'Cate');
    	$pagetitle=empty($pagetitle) ? '新闻列表' : $pagetitle;
    	$this->_list('Article',$where,'id desc',20,'ArtList','ArtPage');

        $this->setseoinfo($pagetitle,$pagetitle,$pagetitle);
        $this->display('Article:list');
    }
    public function read($id=0){
        $id= $id ? $id : intval($_REQUEST['id']);
        $Article=M('Article');
        $vo=$Article->find($id);
        if (!$vo){
            $this->error('该文章不存在或已被删除!');
        }
        $this->assign("vo",$vo);
        if(!empty($vo['tags'])){
            $tags=explode(',',$vo['tags']);
            $where='status=1 and(';
            foreach ($tags as $k => $v) {
                $tagstr.='<a rel="tag" href="tag_'.$v.'.do">'.$v.'</a>';
                $where.=' or tags like "%'.$v.'%" ';
            }
            $where.=' ) ';
            $AboutList=$Article->where($where)->limit(0,10)->order('hit desc,id desc')->select();
        }else{
            $AboutList=$Article->where('status=1')->limit(0,10)->order('hit desc,id desc')->select();
        }
        $this->assign('AboutList',$AboutList);
        $this->assign('tagstr',$tagstr);
        $this->setseoinfo($vo['title'],$vo['title'],utf8Substr(strip_tags($vo['content']),0,200),array('read','article'));
        $Comment=M('Comment');
        $count = $Comment->where('module="Article" and commentid=0 and pid='.$id)->count('id');
        if ($count > 0) {
            //分页显示
            import("@.ORG.Util.Page");
            $p = new Page($count, 20);
            $p->setConfig('widget',array(
                'UpDown'  =>true,
                'PreNext'  =>false,
                'FirstEnd'  =>true,
                'PageLink'  =>true,
                'JumpPage'  =>true
            ));
            $p->setConfig('theme', '%first% %upPage% %linkPage% %downPage% %end% %jumppage%');
            $CommentPage = $p->show();
            $CommentList = $Comment->order('id desc')->where('module="Article" and commentid=0 and pid='.$id)->limit($p->firstRow.','.$p->listRows)->select();
            //echo "|".$Comment->getlastsql()."|";
        }
        foreach ($CommentList as $key => $value) {
            $CommentList[$key]['_child']=$Comment->where('module="Article" and commentid='.$value['id'].' and pid='.$id)->order('id desc')->select();
        }
        $this->assign('CommentList',$CommentList);
        $this->assign('CommentPage',$CommentPage);
        $this->assign('pre',$Article->where("id<".$id)->order("id desc")->find());
        $this->assign('next',$Article->where("id>".$id)->order("id asc")->find());
        $this->display('Article:read');
        $Article->query('update z_article set hit=hit + 1 where id='.$id);
    }
    public function search($qw=''){
        $qw= empty($qw) ? $this->_request('qw') : $qw;
        $kws=explode(' ',$qw);
        $where='status=1';
        if (count($kws)>0){
            foreach ($kws as $k => $v) {
                $where.=" and title like '%".$v."%'";
            }
        }
        $this->setseoinfo('搜索结果:'.$qw,$qw,$qw);
        $this->_list('Article',$where,'id desc',20,'ArtList');
        $this->display('Article:list');
    } 
}
?>